angular.module('factory').controller("ClientEditController", function (
    $scope, $http, $ionicModal, $stateParams, $ionicLoading) {

    $scope.clientInfo = {
        id : '',
        cClientName: '',
        cClientTel: '',
        cClientFax: '',
        cClientBossTel: '',
        cClientBuyerTel: '',
        cClientUseBrands: '',
        fkBands: '',
        clientNameClientDosage: '',
        cClientBuy: '',
        cClientHasTax: '',
        cClientPayWay: '',
        cClientCredit: '',
        cClientSupplier: '',
        cClientRecord: '',
        cClientFeedback: ''
    };


    $scope.formsubmit = function (dirty, invalid) {
        if (dirty == false){
            $ionicLoading.show({
                template: '保存成功',
                noBackdrop: true,
                duration: 1500
            });
        }

        if (invalid == true){
            $ionicLoading.show({
                template: '客户信息填写有误，请检查',
                noBackdrop: true,
                duration: 1500
            });
        }

        $http({
            method: 'POST',
            url: urls.clientsave,
            data: $scope.clientInfo
        }).success(function (data) {
            if (data == 'failure'){
                console.log("保存失败");
                $ionicLoading.show({
                    template: '保存失败',
                    noBackdrop: true,
                    duration: 1500
                });
            }else if(data == 'success'){
                console.log("保存成功");
                $ionicLoading.show({
                    template: '保存成功',
                    noBackdrop: true,
                    duration: 1500
                });
            }
            $state.go('client');

        }).error(function () {
            console.log("保存失败");
            $ionicLoading.show({
                template: '保存失败',
                noBackdrop: true,
                duration: 1500
            });
        });
    };


    /**
     * 弹出选择层
     */
    $ionicModal.fromTemplateUrl('templates/selectbrands.html', {
        scope: $scope
    }).then(function(modal) {
        $scope.modal = modal;
    });


    $scope.comfirmselect = function(list) {
        var result = new Array();
        var resultId = new Array();
        angular.forEach(list, function (item) {
            if (item.checked == true){
                result.push(item.cCaption);
                resultId.push(item.cCode);
            }
        })
        $scope.clientInfo.cClientUseBrands = result.join(',');
        $scope.clientInfo.fkBands = resultId.join(',');
        console.log($scope.clientInfo.fkBands);
        $scope.modal.hide();
    };


    $scope.showBrandsModel = function () {
        $scope.modal.show();
    };


    $scope.devList = [];

    /**
     * 初始化页面根据url的id查询信息客户信息
     * 根据isView判断是否可编辑
     */
    $scope.queryClientDetail = function () {
        var id = $stateParams.id;
        var isview = $stateParams.isview;
        $http.get(urls.clientedit + '/' + id).success(function (data) {
            $scope.clientInfo = data.clients;
            $scope.devList = data.brandList;

            var brands = [];
            angular.forEach($scope.devList, function (brand) {
                if (brand.checked == 'true'){
                    brand.checked = true;
                    brands.push(brand.c_caption);
                }else{
                    brand.checked = false;
                }
            });

            $scope.clientInfo.cClientUseBrands = brands.join(",");
        });
    };

    $scope.queryClientDetail();
});
